λ¬Έμ
μ€νμ΄λ€μ λ§€μΌ λ€λ₯Έ μ·μ μ‘°ν©νμ¬ μ μ΄ μμ μ μμ₯ν©λλ€.
μλ₯Ό λ€μ΄ μ€νμ΄κ° κ°μ§ μ·μ΄ μλμ κ°κ³ μ€λ μ€νμ΄κ° λκ·Έλ μκ²½, κΈ΄ μ½νΈ, νλμ ν°μ μΈ λ₯Ό μ μλ€λ©΄ λ€μλ μ μ²λ°μ§λ₯Ό μΆκ°λ‘ μ κ±°λ λκ·Έλ μκ²½ λμ κ²μ μ κΈλΌμ€λ₯Ό μ°©μ©νκ±°λ ν΄μΌ ν©λλ€.
μ’ λ₯ μ΄λ¦ μΌκ΅΄ λκ·Έλ μκ²½, κ²μ μ κΈλΌμ€ μμ νλμ ν°μ μΈ νμ μ²λ°μ§ κ²μ· κΈ΄ μ½νΈ
μ€νμ΄κ° κ°μ§ μμλ€μ΄ λ΄κΈ΄ 2μ°¨μ λ°°μ΄ clothes κ° μ£Όμ΄μ§ λ μλ‘ λ€λ₯Έ μ·μ μ‘°ν©μ μλ₯Ό return νλλ‘ solution ν¨μλ₯Ό μμ±ν΄μ£ΌμΈμ.
μ νμ¬ν
clothesμ κ° νμ [μμμ μ΄λ¦, μμμ μ’ λ₯]λ‘ μ΄λ£¨μ΄μ Έ μμ΅λλ€.- μ€νμ΄κ° κ°μ§ μμμ μλ 1κ° μ΄μ 30κ° μ΄νμ λλ€.
- κ°μ μ΄λ¦μ κ°μ§ μμμ μ‘΄μ¬νμ§ μμ΅λλ€.
clothesμ λͺ¨λ μμλ λ¬Έμμ΄λ‘ μ΄λ£¨μ΄μ Έ μμ΅λλ€.- λͺ¨λ λ¬Έμμ΄μ κΈΈμ΄λ 1 μ΄μ 20 μ΄νμΈ μμ°μμ΄κ³ μνλ²³ μλ¬Έμ λλ '_' λ‘λ§ μ΄λ£¨μ΄μ Έ μμ΅λλ€.
- μ€νμ΄λ ν루μ μ΅μ ν κ°μ μμμ μ μ΅λλ€.
μ μΆλ ₯ μ
| clothes | return |
|---|---|
| [["yellowhat", "headgear"], ["bluesunglasses", "eyewear"], ["green_turban", "headgear"]] | 5 |
| [["crowmask", "face"], ["bluesunglasses", "face"], ["smoky_makeup", "face"]] | 3 |
π Logic
for (String[] cloth : clothes) {
if (map.containsKey(cloth[1]))
map.put(cloth[1], map.get(cloth[1]) + 1);
else
map.put(cloth[1], 1);
}
- μμ μ’
λ₯λ₯Ό
Keyλ‘, ν΄λΉ μ’ λ₯μ λͺ κ°μ§μ μμμ΄ μ‘΄μ¬νλμ§λ₯Όvalueλ‘ μ μ₯νλ€.
π CODE
μ½λ 보기/μ κΈ°π«
import java.util.*;
class Solution {
public int solution(String[][] clothes) {
HashMap<String, Integer> map = new HashMap<>();
for (String[] cloth : clothes) {
if (map.containsKey(cloth[1]))
map.put(cloth[1], map.get(cloth[1]) + 1);
else
map.put(cloth[1], 1);
}
int answer = 1;
for (Map.Entry<String, Integer> entry : map.entrySet())
answer *= (entry.getValue() + 1);
return answer - 1;
}
}
βοΈ Review
HashMapμKeyμValueλ₯Ό μ΄λ»κ² νμ©ν μ§λ λ μ¬λ Έμ§λ§, μ λ΅ λμΆμ μν μμ λͺ¨λ₯΄κ² μ΄μ μ§λ¬Έ κ²μνμ μ°Έκ³ νλ€.- λ¨μν μ‘°ν©μμ΄κΈ΄ νλ°... κΈ°μ΅μ΄ μ λ¬λ€;
π μΆμ²
Programmers : https://programmers.co.kr/learn/courses/30/lessons/42578